//
//  JKScheduleView.m
//  JKSchedule
//
//  Created by 王冲 on 2019/4/1.
//  Copyright © 2019年 JK科技有限公司. All rights reserved.
//

#import "JKScheduleView.h"
#import "NSLayoutConstraint+FF.h"
#import "UIView+JKLayout.h"
#import "JKScheduleCollectionViewCell.h"

#define JKWidth [UIScreen mainScreen].bounds.size.width
#define JKHeight [UIScreen mainScreen].bounds.size.height
#define CollectonX 30
@interface JKScheduleView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionViewFlowLayout *flowLayout;
}

// 展示 不合格
@property(nonatomic,strong) UIButton *showBtn1;

// 展示 优秀
@property(nonatomic,strong) UIButton *showBtn2;

// 展示 优秀度
@property(nonatomic,strong) UIButton *showBtn3;

@end

@implementation JKScheduleView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUICollectionView];
    }
    
    return self;
}

#pragma mark 设置一个collection
-(void)setUICollectionView{
    
    // 1.  进度条
    flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.collectionView registerClass:[JKScheduleCollectionViewCell class] forCellWithReuseIdentifier:@"ID"];
    [self addSubview:self.collectionView];
    
    // 2.  移动标签 不合格 优秀 卓越
    [self addSubview:self.showBtn1];
    [self addSubview:self.showBtn2];
    [self addSubview:self.showBtn3];
    
    // 4.  0  12  30
    [self addSubview:[self setLabelX:CGRectGetMinX(self.collectionView.frame)-20 withLabelY:CGRectGetMaxY(self.collectionView.frame)+10 withLabelWidth:60 withLabelHeight:20 withTittle:@"0" withTittleFont:15.f]];
    CGFloat radiusWidth = ([UIScreen mainScreen].bounds.size.width-CollectonX*2)/(long)[self getNumberOfDaysInMonth];
    [self addSubview:[self setLabelX:CGRectGetMinX(self.collectionView.frame)+(12*radiusWidth) withLabelY:CGRectGetMaxY(self.collectionView.frame)+10 withLabelWidth:50 withLabelHeight:20 withTittle:@"12" withTittleFont:15.f]];
    [self addSubview:[self setLabelX:CGRectGetMaxX(self.collectionView.frame)-100 withLabelY:CGRectGetMaxY(self.collectionView.frame)+10 withLabelWidth:50 withLabelHeight:20 withTittle:@"20" withTittleFont:15.f]];//[NSString stringWithFormat:@"%ld",[self getNumberOfDaysInMonth]]
    
}

#pragma mark 多个Label的统一合并
-(UILabel *)setLabelX:(CGFloat)labelX withLabelY:(CGFloat)labelY withLabelWidth:(CGFloat)labelWidth withLabelHeight:(CGFloat)labelHeight withTittle:(NSString *)tittle withTittleFont:(CGFloat)font{
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(labelX, labelY, labelWidth, labelHeight)];
    label.text = tittle;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:font];
    
    return label;
}



/*
 collection的段落
 */
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
/*
 collection每个段落格子数
 */
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.count;
    
}
/*
 格子的宽高设置
 */
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat radiusWidth = ([UIScreen mainScreen].bounds.size.width-CollectonX*2)/(long)[self getNumberOfDaysInMonth];
    
    return CGSizeMake(radiusWidth, 10);
}

//列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 1.f;
}

//行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0.f;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

/*
 UICollectionViewCell的具体的数据加载
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JKScheduleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
    
    return cell;
    
}

-(UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(CollectonX,(self.frame.size.height-10)/2.0, JKWidth-CollectonX*2, 10) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.layer.cornerRadius = 5;
        _collectionView.clipsToBounds = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.alwaysBounceVertical = NO;
        _collectionView.alwaysBounceHorizontal = NO;
        _collectionView.pagingEnabled = NO;
    }
    
    return _collectionView;
}

-(UIButton *)showBtn1{
    
    if (!_showBtn1) {
        
        CGFloat width = [self string:@"不合格" sizeWithFont:[UIFont systemFontOfSize:15.f] maxSize:CGSizeMake(200, 20)].width;
        _showBtn1 = [[UIButton alloc]initWithFrame:CGRectMake(CollectonX-width/2.0, (self.frame.size.height-10)/2.0-25-10, width+20, 25)];
        //        [_showBtn1 setBackgroundImage:image forState:UIControlStateNormal];
        [_showBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_showBtn1 setTitle:@"不合格" forState:UIControlStateNormal];
        // [_showBtn1 setBackgroundColor:[UIColor brownColor]];
        _showBtn1.titleLabel.font = [UIFont systemFontOfSize:15.f];

        
    }
    return _showBtn1;
}

-(UIButton *)showBtn2{
    
    if (!_showBtn2) {
        
        CGFloat radiusWidth = ([UIScreen mainScreen].bounds.size.width-CollectonX*2)/(long)[self getNumberOfDaysInMonth];
        
        CGFloat width = [self string:@"优秀" sizeWithFont:[UIFont systemFontOfSize:15.f] maxSize:CGSizeMake(200, 20)].width;
        _showBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(CollectonX+radiusWidth*15, (self.frame.size.height-10)/2.0-25-10, width+20, 25)];
        [_showBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_showBtn2 setTitle:@"优秀" forState:UIControlStateNormal];
        _showBtn2.titleLabel.font = [UIFont systemFontOfSize:15.f];
        // [_showBtn2 setBackgroundColor:[UIColor brownColor]];
    }
    return _showBtn2;
}

-(UIButton *)showBtn3{
    
    if (!_showBtn3) {
        
        CGFloat width = [self string:@"卓越" sizeWithFont:[UIFont systemFontOfSize:15.f] maxSize:CGSizeMake(200, 20)].width;
        _showBtn3 = [[UIButton alloc]initWithFrame:CGRectMake(JKWidth-CollectonX-width/2.0, (self.frame.size.height-10)/2.0-25-10, width+20, 25)];
        [_showBtn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_showBtn3 setTitle:@"卓越" forState:UIControlStateNormal];
        _showBtn3.titleLabel.font = [UIFont systemFontOfSize:15.f];
        // [_showBtn3 setBackgroundColor:[UIColor brownColor]];
    }
    return _showBtn3;
}

// 获取当月的天数
- (NSInteger)getNumberOfDaysInMonth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
    NSUInteger numberOfDaysInMonth = range.length;
    
    return numberOfDaysInMonth;
}

-(void)setCount:(NSUInteger)count{
    
    _count = count;
    
//    NSLog(@"count=%lu",(unsigned long)count);
    
    NSString *tittle;
    
    if (count<12) {
        tittle = @"不合格";
    }else if (count >=20){
        tittle = @"卓越";
    }else{
        tittle = @"优秀";
    }
    
    CGFloat showWidth = [self string:tittle sizeWithFont:[UIFont systemFontOfSize:15.f] maxSize:CGSizeMake(200, 20)].width;
    
    CGFloat radiusWidth = ([UIScreen mainScreen].bounds.size.width-CollectonX*2)/(long)[self getNumberOfDaysInMonth];
    
    CGFloat x;
    if (count == 0) {
        
        x = CGRectGetMinX(self.collectionView.frame)-showWidth/2.0;
    }else{
        x = CGRectGetMinX(self.collectionView.frame)+count*radiusWidth-showWidth/2.0-radiusWidth/2.0;
    }
    
    CGRect rect = CGRectMake(x, (self.frame.size.height-10)/2.0-25-10, showWidth+15,25);
    
    // 防止进度变化错乱
    self.showBtn1.x = CollectonX-showWidth/2.0;
    [self.showBtn1 setBackgroundImage:[UIImage new] forState:UIControlStateNormal];
    self.showBtn2.x = CollectonX+radiusWidth*12;
    [self.showBtn2 setBackgroundImage:[UIImage new] forState:UIControlStateNormal];
    self.showBtn3.x = JKWidth-100-CollectonX;
    [self.showBtn3 setBackgroundImage:[UIImage new] forState:UIControlStateNormal];
    
    // 防止拉伸处理
    UIImage *image = [UIImage imageNamed:@"arrows_title"];
    image = [image stretchableImageWithLeftCapWidth:floorf(0) topCapHeight:0];
    
    if ([tittle isEqualToString:@"不合格"]) {
        
        [self.showBtn1 setBackgroundImage:image forState:UIControlStateNormal];
        self.showBtn1.frame = rect;
        
    }else if ([tittle isEqualToString:@"优秀"]){
        [self.showBtn2 setBackgroundImage:image forState:UIControlStateNormal];
        self.showBtn2.frame = rect;
    }else{
        
        self.showBtn2.x = CollectonX+radiusWidth*11;
        [self.showBtn2 setBackgroundImage:[UIImage new] forState:UIControlStateNormal];
        
        // 卓越
        [self.showBtn3 setBackgroundImage:image forState:UIControlStateNormal];
        self.showBtn3.frame = rect;
    }
    
    [self.collectionView reloadData];
}

//确定高度的设置
-(CGSize)string:(NSString *)string sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize{
    
    NSDictionary *attrs = @{NSFontAttributeName:font};
    return [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading |NSLineBreakByCharWrapping attributes:attrs context:nil].size;
}

@end
